import { MemoryManager } from "GameLoop/DecisionMaking/MemoryManager";
import { AllEntityTypes, EntityType, IEntity } from "./Entity.interface";
import { MineEntity } from "./room/mine/MineEntity";

interface EntityGetter
{
    getEntityByID<T extends ABaseEntity<INoMemory>>(id: string): T ;
    getEntity<T extends EntityType>(type: T, region: string): AllEntityTypes[T] ;
    getEntitys<T extends EntityType>(type: T, region: string): AllEntityTypes[T][] ;
    removeEntityByID(id: string): boolean
}

export abstract class ABaseEntity<U extends INoMemory ,I extends IEntityInitializer=IEntityInitializer > implements IEntity<U>
{
    
    
    private _entityGetter!:EntityGetter;
    private _type!:EntityType;
    protected _initializer!:I;
      /**
     * 初始化
     */ 
    public initialize(entityGetter:EntityGetter,initializer:I)
    {
        this._entityGetter = entityGetter;
        this._type = this.constructor.name as EntityType;
        this._initializer = initializer;
        // this.initAttributes();
        MemoryManager.allocHeap(this.GUID());
    }
    abstract initAttributes():void

    // 添加一个销毁的接口

    public toJSON()
    {
        return {_type:this._type,_initializer:this._initializer}
    }
    public  entityType(): EntityType {
        return this._type
    }
    // public abstract GUID:string;    // 全局唯一ID
    // public abstract  key:string;     // 在region内是唯一
    public   GUID(): string
    {
        return this._initializer.GUID;
    }
    public get key(): string
    {
        return this._initializer.key;
    }
    
 
    public abstract  region:string;   // 实体所在区域
    // public type!:EntityType;    // 类型
   
    getEntityByID<T extends ABaseEntity<INoMemory>>(id: string): T 
    {
       return this._entityGetter.getEntityByID<T>(id);
    }
    getEntity<T extends EntityType>(type: T, region: string = this.region): AllEntityTypes[T] {
        return this._entityGetter.getEntity(type,region);
    }
    getEntitys<T extends EntityType>(type: T, region: string= this.region): AllEntityTypes[T][] {
        return this._entityGetter.getEntitys(type,region);
    }
    removeEntityByID(id: string): boolean {
        return this._entityGetter.removeEntityByID(id);
    }
    public log(type:LogType,...arg:any[]):void
     {
         global.Log.Text(this.GUID(),arg.join("\t"),type);
     }
    abstract getNodeMemory(): U
    // abstract findRegionTaskById<T>(id: string, region?: string | undefined): T ;
    public getNodeHeap<T extends any>(): T{
        return MemoryManager.getHeap(this.GUID());
    }
   
     // 小队被清理
    public onDestory(): boolean 
    {
        const regionCache = MemoryManager.getMemory<IMemoryCache>(this.region);
        if(!regionCache.Nodes ||!regionCache.Nodes[this.key])
        {
            this.log(`error`,(`[${this.key}],${this.GUID()}->${this.entityType()} 的内存未初始化`));
        }
        regionCache.Nodes[this.key] = undefined;
        this.log(`warn`,`内存清除`,this.key,this.region);
        delete regionCache.Nodes[this.key];
        MemoryManager.deleteNodeHeap(this.GUID())
        return true;
    }
    public clearAction():void
    {
       (global.GameLoop as any).workerManager.actionManager.clearAction(this.GUID());
    }
}